home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / ObjectPool.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  2.5 KB  |  86 lines  |  [TEXT/CWIE]

  1. // ObjectPool.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5.  
  6. package netscape.application;
  7.  
  8. class ObjectPool {
  9.     Object freePool[];
  10.     int    freePoolMaxLength;
  11.     int    freePoolNextSlot;
  12.     Class  objectClass;
  13.     int    allocSaved;
  14.     int    allocDone;
  15.     int    maxCapacity;
  16.  
  17.     public ObjectPool(String className) {
  18.         this(className,32);
  19.     }
  20.  
  21.     public ObjectPool(String className,int aMaxCapacity) {
  22.         super();
  23.         freePool = new Object[1];
  24.         freePoolMaxLength = 1;
  25.         freePoolNextSlot  = 0;
  26.         try {
  27.             objectClass = Class.forName(className);
  28.         } catch( ClassNotFoundException e ) {
  29.             System.out.println("ObjectPool cannot find class " + className);
  30.         }
  31.         allocSaved=0;
  32.         allocDone = 0;
  33.         maxCapacity = aMaxCapacity;
  34.     }
  35.  
  36.     public Object allocateObject() {
  37.         Object result = null;
  38.  
  39.         synchronized( this ) {
  40.             if( freePoolNextSlot > 0 ) {
  41.                 freePoolNextSlot--;
  42.                 result = freePool[freePoolNextSlot];
  43.             }
  44.         }
  45.  
  46.         if( result == null ) {
  47.             allocDone++;
  48.             try {
  49.                 result = objectClass.newInstance();
  50.             } catch( InstantiationException e) {
  51.                 System.out.println("Cannot instantiate instance of class " + objectClass);
  52.             } catch( IllegalAccessException e ) {
  53.                 System.out.println("Cannot instantiate instance of class. Illegal." + objectClass);
  54.             }
  55.         } else
  56.             allocSaved++;
  57.         return result;
  58.     }
  59.  
  60.     public void recycleObject(Object anObject) {
  61.         synchronized( this ) {
  62.             if( freePoolMaxLength < maxCapacity ) {
  63.                 if( freePoolNextSlot == freePoolMaxLength ) {
  64.                     Object newFreePool[] = new Object[freePoolMaxLength * 2];
  65.                     System.arraycopy(freePool,0,newFreePool,0, freePoolMaxLength);
  66.                     freePool = newFreePool;
  67.                     freePoolMaxLength *= 2;
  68.                 }
  69.                 freePool[freePoolNextSlot++] = anObject;
  70.             }
  71.         }
  72.     }
  73.  
  74.     protected void finalize() {
  75.         int i;
  76.         for(i=0 ; i < freePoolNextSlot ; i++)
  77.             freePool[i] = null;
  78.         freePool=null;
  79.     }
  80.  
  81.     public String toString() {
  82.         return "Object pool for class " + objectClass + " has " + freePoolNextSlot + " instances." +
  83.             " " + allocSaved + " allocations avoided allocation performed:" + allocDone;
  84.     }
  85. }
  86.